package com.housediyadmin.service
{
	import com.housediyadmin.events.brandEvents.GetBrandEvent;
	import com.housediyadmin.vo.Brand;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2010/12/15
	 */	
	public class BrandRemoteObjectService extends Actor
	{
		public var brandRemoteObject:RemoteObject;
		
		public function BrandRemoteObjectService()
		{
			super();
			var amfChannel:Array = [new AMFChannel(null, "http://localhost/housediyadmin/Gateway.aspx")];
			//var amfChannel:Array = [new AMFChannel(null, "http://10.60.1.120/HouseDiyAdmin/Gateway.aspx")];
			var channelSet:ChannelSet = new ChannelSet();
			channelSet.channels = amfChannel;
			
			brandRemoteObject = new RemoteObject("fluorine");
			brandRemoteObject.source = "HouseDiyService.HouseDiyAction";
			brandRemoteObject.showBusyCursor = true;
			brandRemoteObject.channelSet = channelSet;
			
			brandRemoteObject.getOperation("getAllBrands").addEventListener(ResultEvent.RESULT, getAllBrandsResult);
			brandRemoteObject.getOperation("getAllBrands").addEventListener(FaultEvent.FAULT, faultHandler);
			
			brandRemoteObject.getOperation("deleteBrandByIDs").addEventListener(ResultEvent.RESULT, deleteBrandByIDResult);
			brandRemoteObject.getOperation("deleteBrandByIDs").addEventListener(FaultEvent.FAULT, faultHandler);
			
			brandRemoteObject.getOperation("updateBrand").addEventListener(ResultEvent.RESULT, editBrandResult);
			brandRemoteObject.getOperation("updateBrand").addEventListener(FaultEvent.FAULT, faultHandler);
			
			brandRemoteObject.getOperation("addABrand").addEventListener(ResultEvent.RESULT, addBrandResult);
			brandRemoteObject.getOperation("addABrand").addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		public function getAllBrand():void
		{
			brandRemoteObject.getAllBrands();
		}
		
		public function deleteBrandByID(brandID:Array):void
		{
			brandRemoteObject.deleteBrandByIDs(brandID);
		}
		
		public function editBrand(brand:Brand):void
		{
			brandRemoteObject.updateBrand(brand);
		}
		
		public function addBrand(brand:Brand):void
		{
			brandRemoteObject.addABrand(brand);
		}
		
		private function getAllBrandsResult(event:ResultEvent):void
		{
			dispatch(new GetBrandEvent(GetBrandEvent.UPDATE_BRANDS_MODEL, event.result as ArrayCollection));
		}
		
		private function deleteBrandByIDResult(event:ResultEvent):void
		{
			dispatch(new GetBrandEvent(GetBrandEvent.UPDATE_BRANDS_MODEL, event.result as ArrayCollection));
		}
		
		private function editBrandResult(event:ResultEvent):void
		{
			dispatch(new GetBrandEvent(GetBrandEvent.UPDATE_BRANDS_MODEL, event.result as ArrayCollection));
		}
		
		private function addBrandResult(event:ResultEvent):void
		{
			dispatch(new GetBrandEvent(GetBrandEvent.UPDATE_BRANDS_MODEL, event.result as ArrayCollection));
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(" fault :" + event.toString());
		}
	}
}